package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.base.domain.PrivateMessage;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelCommendQuery;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.IPrivateMessageService;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 游记资源控制器
 */
@RestController
@RequestMapping("privateMessages")
public class PrivateMessageController {

    @Autowired
    private IPrivateMessageService messageService;

    /**
     * 保存发送的信息数据
     * @param privateMessage
     * @return
     */
    @PostMapping
    public JSONResult save(PrivateMessage privateMessage){
        messageService.insert(privateMessage);
        return new JSONResult();
    }

    @GetMapping("states")
    public Object getPrivateNumber(Long id){
        Integer count = messageService.selectPrivateNumberByUserId(id);
        return count;
    }

    @GetMapping("privateTotals")
    public JSONResult changePrivateTotal( Long userId){
        messageService.changePrivateTotal(userId);
        return new JSONResult();
    }

    /**
     * 根据发送者id和接受者id查询聊天内容
     * @return
     */
    @GetMapping("{receiveId}")
    public List<PrivateMessage> getContentBySendIdAndReceiveId(@PathVariable Long receiveId,Long sendId){
        List<PrivateMessage> list1 = messageService.selectContentBySendIdAndReceiveId(receiveId, sendId);
        List<PrivateMessage> list2 = messageService.selectContentBySendIdAndReceiveId(sendId, receiveId);
        list1.addAll(list2);
        return list1;
    }



    @GetMapping
    public List<PrivateMessage> get(Long receiveId,Long sendId){
        List<PrivateMessage> list = messageService.selectByUserWeiDu(receiveId,sendId);
        if(list != null){
            for (PrivateMessage message : list) {
                messageService.changeReceiveState(message.getId());
            }
        }
        return list;
    }

}
